/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.basic.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedPersonAuditDTO;
import com.hopes.bizz.close.basic.dto.ClosedPersonDTO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.basic.entity.ClosedPersonEntity;
import com.hopes.bizz.close.basic.dto.ClosedPersonExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedPersonExcelVO;
import com.hopes.bizz.close.basic.service.ClosedPersonService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 园区人员信息
 *
 * @author pig
 * @date 2023-10-23 11:21:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/person")
@Tag(description = "person", name = "园区人员信息管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedPersonController {

    private final ClosedPersonService closedPersonService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param closedPersonDTO 园区人员信息
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_person_view')")
    public R getClosedPersonPage(Page page, ClosedPersonDTO closedPersonDTO) {
        LambdaQueryWrapper<ClosedPersonEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(closedPersonDTO.getOrgId()), ClosedPersonEntity::getOrgId, closedPersonDTO.getOrgId());
        wrapper.eq(StrUtil.isNotBlank(closedPersonDTO.getUseFlag()), ClosedPersonEntity::getUseFlag, closedPersonDTO.getUseFlag());
        wrapper.eq(Objects.nonNull(closedPersonDTO.getPersonType()), ClosedPersonEntity::getPersonType, closedPersonDTO.getPersonType());
        wrapper.eq(Objects.nonNull(closedPersonDTO.getAuditStatus()), ClosedPersonEntity::getAuditStatus, closedPersonDTO.getAuditStatus());
        wrapper.like(StrUtil.isNotBlank(closedPersonDTO.getKeyword()), ClosedPersonEntity::getName, closedPersonDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonDTO.getKeyword()), ClosedPersonEntity::getPhone, closedPersonDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedPersonDTO.getKeyword()), ClosedPersonEntity::getIdNum, closedPersonDTO.getKeyword());
        wrapper.orderByAsc(ClosedPersonEntity::getOrgId).orderByDesc(ClosedPersonEntity::getAuditStatus);
        return R.ok(closedPersonService.page(page, wrapper));
    }


    /**
     * 通过id查询园区人员信息
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_person_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(closedPersonService.getById(id));
    }

    /**
     * 新增园区人员信息
     *
     * @param closedPerson 园区人员信息
     * @return R
     */
    @Operation(summary = "新增园区人员信息", description = "新增园区人员信息")
    @SysLog("新增园区人员信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_person_add')")
    public R save(@RequestBody ClosedPersonEntity closedPerson) {
        return R.ok(closedPersonService.save(closedPerson));
    }

    /**
     * 修改园区人员信息
     *
     * @param closedPerson 园区人员信息
     * @return R
     */
    @Operation(summary = "修改园区人员信息", description = "修改园区人员信息")
    @SysLog("修改园区人员信息")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_person_edit')")
    public R updateById(@RequestBody ClosedPersonEntity closedPerson) {
        return R.ok(closedPersonService.updateById(closedPerson));
    }

    /**
     * 审核园区人员信息
     *
     * @param closedPersonAuditDTO 园区人员信息
     * @return R
     */
    @Operation(summary = "审核园区人员信息", description = "审核园区人员信息")
    @SysLog("审核园区人员信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('basic_person_audit')")
    public R auditById(@RequestBody ClosedPersonAuditDTO closedPersonAuditDTO) {
        return R.ok(closedPersonService.auditById(closedPersonAuditDTO));
    }

    /**
     * 通过id删除园区人员信息
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除园区人员信息", description = "通过id删除园区人员信息")
    @SysLog("通过id删除园区人员信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_person_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedPersonService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('basic_person_export')")
    public List<ClosedPersonExcelVO> template() {
        return closedPersonService.exportExcel(Collections.singletonList(new ClosedPersonEntity()));
    }

    /**
     * 导出excel 表格
     *
     * @param closedPersonExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_person_export')")
    public List<ClosedPersonExcelVO> export(ClosedPersonExcelDTO closedPersonExcelDto) {
        return closedPersonService.exportExcel(closedPersonService.list());
    }

    /**
     * 导入excel 表格
     *
     * @param closedPersonExcelDtoList 查询条件
     * @param bindingResult            错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('basic_person_export')")
    public R importExcel(@RequestExcel List<ClosedPersonExcelDTO> closedPersonExcelDtoList, BindingResult bindingResult) {
        return closedPersonService.importExcel(closedPersonExcelDtoList, bindingResult);
    }
}
